package net.pulseproject.router.config.instantiator.builder;

import net.pulseproject.router.config.instantiator.builder.validator.FieldValidator;
import net.pulseproject.router.config.instantiator.builder.validator.SkeletonFieldValidator;
import net.pulseproject.router.modules.logic.beatstabilizer.BeatStabilizerConfig;
import net.pulseproject.router.modules.useable.BeatStabilizerModule;


public class BeatStabilizerModuleBuilder extends BiDirectionalModuleBuilder<BeatStabilizerModule> {
	
	private static final int DEFAULT_SUSTAINED_FIRE_WINDOW = 20;
	
	private Integer globalMinMs;
	private Integer globalMaxMs;
	private Float frameDeviationPercent;
	private Integer sustainedFireWindow = Integer.valueOf(DEFAULT_SUSTAINED_FIRE_WINDOW);
	
	private final FieldValidator validator = new SkeletonFieldValidator() {
		@SuppressWarnings("synthetic-access")
		@Override
		protected void validateFields() {
			if(BeatStabilizerModuleBuilder.this.globalMinMs == null) {
				throw new RuntimeException("globalMinMs not yet set");
			}
			if(BeatStabilizerModuleBuilder.this.globalMaxMs == null) {
				throw new RuntimeException("globalMaxMs not yet set");
			}
			if(BeatStabilizerModuleBuilder.this.frameDeviationPercent == null) {
				throw new RuntimeException("frameDeviationPercent not yet set");
			}
		}
	};
	
	public BeatStabilizerModuleBuilder() {
		this.addValidator(this.validator);
	}
	
	public final void setGlobalMinMs(Integer globalMinMs) {
		this.globalMinMs = globalMinMs;
	}
	public final void setGlobalMaxMs(Integer globalMaxMs) {
		this.globalMaxMs = globalMaxMs;
	}
	public final void setFrameDeviationPercent(Float frameDeviationPercent) {
		this.frameDeviationPercent = frameDeviationPercent;
	}
	public final void setSustainedFireWindow(Integer sustainedFireWindow) {
		this.sustainedFireWindow = sustainedFireWindow;
	}

	@Override
	protected final BeatStabilizerModule buildModule() {
		return new BeatStabilizerModule(
			this.getId(),
			this.getOutgoingModules(),
			new BeatStabilizerConfig(
				this.globalMinMs.intValue(),
				this.globalMaxMs.intValue(),
				this.frameDeviationPercent.doubleValue()
			),
			this.sustainedFireWindow.intValue()
		);
	}
	
}
